package cn.virens.docs;

import java.sql.ResultSet;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import cn.hutool.core.io.FileUtil;
import cn.virens.common.StringUtil;
import cn.virens.common.db.DatabaseManagerImpl;

public class DatabaseDoc {
	private static final String sql1 = "SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='%s' AND TABLE_NAME LIKE '%s'";
	private static final String sql2 = "SELECT COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,COLUMN_COMMENT FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'";

	private final DatabaseManagerImpl dbManager1 = new DatabaseManagerImpl();

	private static final String path = "C:/Users/ccqlk-loioi/Desktop/";
	private static final String schema = "ccqlk_ludeng";

	@BeforeEach
	public void initialize() throws Exception {
		this.dbManager1.setJdbcUrl("jdbc:mysql://192.168.0.10:3306/ccqlk_ludeng?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true");
		this.dbManager1.setJdbcDriverClass("com.mysql.cj.jdbc.Driver");
		this.dbManager1.setJdbcUsername("root");
		this.dbManager1.setJdbcPassword("root");
		this.dbManager1.initialize();
	}

	@Test
	public void build() throws Exception {
		this.dbManager1.execute((con) -> {
			ResultSet res = con.executeQuery(sql1, schema, "%");

			StringBuilder builder = new StringBuilder();

			while (res.next()) {
				String table = res.getString(1);
				String name = res.getString(2);

				builder.append(String.format("\r\n<h1>%s(%s)</h1>", name, table));
				builder.append("\r\n<table style=\"width:100%;\">");
				builder.append("\r\n    <tbody>");
				builder.append("\r\n        <tr>");
				builder.append("\r\n            <th>字段</th>");
				builder.append("\r\n            <th>字段名</th>");
				builder.append("\r\n            <th>类型</th>");
				builder.append("\r\n            <th>可否为空</th>");
				builder.append("\r\n            <th>默认值</th>");
				builder.append("\r\n        </tr>");

				// 读取全部字段
				ResultSet res1 = con.executeQuery(sql2, schema, table);
				while (res1.next()) {
					builder.append("\r\n        <tr>");
					builder.append("\r\n            <td>" + s(res1.getString(1)) + "</td>");
					builder.append("\r\n            <td>" + s(res1.getString(5)) + "</td>");
					builder.append("\r\n            <td>" + s(res1.getString(4)) + "</td>");
					builder.append("\r\n            <td>" + s(res1.getString(3)) + "</td>");
					builder.append("\r\n            <td>" + s(res1.getString(2)) + "</td>");
					builder.append("\r\n        </tr>");
				}

				builder.append("\r\n    </tbody>");
				builder.append("\r\n</table>");

			}

			// 保存到磁盘
			FileUtil.writeString(builder.toString(), path + schema + ".txt", "utf-8");
		});
	}

	private static String s(String str) {
		return str == null ? "" : StringUtil.replace(str, "&", "&amp;");
	}
}
